package com.redrose.flutterdemo.camera;

import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


import com.alibaba.android.arouter.facade.annotation.Route;
import com.redrose.flutterdemo.R;
import com.redrose.flutterdemo.bean.Photo;
import com.redrose.flutterdemo.databinding.ActivityPreviewBinding;
import com.redrose.flutterdemo.pagetransformer.RotateDownPageTransformer;
import com.redrose.flutterdemo.ui.activitys.BaseActivity;
import com.redrose.flutterdemo.utils.GlideUtils;
import com.redrose.flutterdemo.view.ResizableImageView;

import java.util.List;
@Route(path = "/activity/preview")
public class PreviewActivity extends BaseActivity<ActivityPreviewBinding> {

    private int mCurrentMax;
    private List<Photo> mPhotoList;
    private ImagePagerAdapter mAdapter;
    private int mPosition;

    @Override
    public int setLayout() {
        return R.layout.activity_preview;
    }

    private void initEvent() {
        mBinding.imageViewPager.setOnScrollChangeListener(new View.OnScrollChangeListener() {
            @Override
            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {

            }
        });
    }

    private void initAdapter() {
        mAdapter = new ImagePagerAdapter();
        mBinding.imageViewPager.setAdapter(mAdapter);
        /** 翻页动画设置 */
        /*mBinding.imageViewPager.setPageTransformer(false,new ZoomOutPageTransformer());*/
        /*mBinding.imageViewPager.setPageTransformer(false,new DepthPageTransformer());*/
        mBinding.imageViewPager.setPageTransformer(false, new RotateDownPageTransformer());
        mBinding.imageViewPager.setCurrentItem(mPosition);
    }
    @Override
    public void initData() {
        mPhotoList = (List<Photo>) getIntent().getBundleExtra("data").getSerializable("photoList");
        mCurrentMax = mPhotoList.size();
        initAdapter();
        initEvent();
    }

    private class ImagePagerAdapter extends PagerAdapter {

        @Override
        public int getCount() {

            return mCurrentMax;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.image_viewpager_item, null);
            ResizableImageView imageView = view.findViewById(R.id.resize_image);
            GlideUtils.load(imageView, mPhotoList.get(position).getPath());
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
}
